package com.sinosoft.ptas.system.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ylmylm
 */
public class EntityListConverter {
	/**
	 * 把数据库得来得list-----------[[name0, 0], [name1, 1], [name2, 2]]
	 * 转化为传给前端显示的格式 list----[[name0, name1, name2], [0, 1, 2]]
	 * @param className 持久化层实体类的'名称'
	 * @param lists 持久化层的list(从数据库中取出)----[name0, 0], 
	 * 										  [name1, 1],
	 * 										  [name2, 2]
	 * @return List 传给前端的数据-----------------[
	 * 										   [name0, name1, name2], 
	 * 										   [0, 1, 2]]
	 * 										  ]
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<?> convertToList(String className, List<?> lists) throws Exception {
		List<ArrayList<?>> resultLists = new ArrayList<ArrayList<?>>();
		Class<?> clazz = Class.forName(className);
		Field[] fields = clazz.getDeclaredFields();
		
		for (Field field : fields) {
			ArrayList list = new ArrayList();
			String fieldName = field.getName();
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String methodName = "get" + firstLetter + fieldName.substring(1);
			Method fieldMethod = clazz.getMethod(methodName, new Class[]{});
			for (Object entity : lists) {
				Object value = fieldMethod.invoke(entity, new Object[]{});
				list.add(value);
			}
			resultLists.add(list);
		}		
		return resultLists;
	}
	/**
	 * 
	 * @param className
	 * @param lists
	 * @return{name=[name0, name1, name2, name3, name4, name5, name6, name7, name8, name9], 
	 * 		   count=[0, 2, 4, 6, 8, 10, 12, 14, 16, 18], 
	 * 		   age=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public static Map convertToMap(String className, List<?> lists, String... args) {
		Map<String, List<?>> map = new HashMap<>();
		Class<?> clazz = null;
		try {
			//得到类名代表的类对象
			clazz = Class.forName(className);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			ArrayList<Object> list = new ArrayList<>();
			String fieldName = field.getName();
			
			//判断是否为serialVersionUID，因为serialVersionUID没有set和get方法
			Boolean isContinue = true;
			for(String arg : args) {
				if(arg.equals(field.getName())) {
					isContinue = false;
					break;
				}
			}
			if(!isContinue) {
				continue;
			}
			
			//得到属性第一个字母转换大写
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			//拼接方法名
			String methodName = "get" + firstLetter + fieldName.substring(1);
			
			try {
				//用方法名得到方法
				Method fieldMethod = clazz.getMethod(methodName, new Class[]{});
				for (Object entity : lists) {
					Object value = fieldMethod.invoke(entity, new Object[]{});
					list.add(value);
				}
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			map.put(fieldName, list);
			//resultLists.add(list);
		}
		return map;
	}
	
	/**
	 * 当数据库中查询的结果集为空时，提供给前端，让前端正确显示
	 * @param className 结果集的类名
	 * @param args 序列化参数字符串"serialVersionUID"
	 * @return 返回的map的空属性值，如{myMonth=null, count=null, myRegion=null}
	 */
	@SuppressWarnings("rawtypes")
	public static Map nullConvertToMap(String className, String... args) {
		Map<String, List<?>> map = new HashMap<>();
		Class<?> clazz = null;
		try {
			//得到类名代表的类对象
			clazz = Class.forName(className);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			//ArrayList<?> list = new ArrayList<>();
			String fieldName = field.getName();
			
			//判断是否为serialVersionUID，因为serialVersionUID没有set和get方法
			Boolean isContinue = true;
			for(String arg : args) {
				if(arg.equals(field.getName())) {
					isContinue = false;
					break;
				}
			}
			if(!isContinue) {
				continue;
			}
			//为属性赋值为null
			map.put(fieldName, null);
		}
		return map;
	}
	
	/**
	 * 将list转化成Map,
	 * 处理null和非null两种情况的map
	 * @param className
	 * @param entityLists
	 * @param serialVersionUID
	 * @return 返回map
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, List<?>> toMap(String className, List<?> entityLists, String serialVersionUID) {
		Map<String, List<?>> map = new HashMap<>();
		//返回list中的结果的个数
		int count = entityLists.size();
		List<Integer> countList = new ArrayList<>();
		countList.add(count);
		if(entityLists!=null && !entityLists.isEmpty()) {
			map = EntityListConverter.convertToMap(className, entityLists, serialVersionUID);
		} else {
			map = EntityListConverter.nullConvertToMap(className, serialVersionUID);
		}
		map.put("count", countList);
		return map;	
	}
}
